<?php

namespace app\common\service;

use app\web\service\AstroCard;
use think\Cache;

class AstroCardService
{
    /**
     * 星座卡片 - 属性卡
     * @return array
     */
    public static function getCardDetail(array $recodeInfo)
    {
        $recodeHashStr = md5(json_encode($recodeInfo, JSON_UNESCAPED_UNICODE));
        $cacheKey = 'astro:astro_card:card_detail:' . $recodeInfo['id'] . ':' . $recodeHashStr;
        $resData = Cache::get($cacheKey, false);
        if (!$resData)
        {   // 缓存
            $res = (new AstroCard())->getCardData($recodeInfo);
            $power = [];
            foreach ($res['power'] as $key => $re)
            {
                $num = str_split($re['score']);
                $len = count($num);
                $re['score1'] = "";
                $re['score2'] = "";
                $re['score3'] = "";
                if ($re['id'] === 0)
                {
                    $re['score1'] = "/zhunlemini/astro_card/$num[0]-red.png";
                    if ($len > 1)
                    {
                        $re['score2'] = "/zhunlemini/astro_card/$num[1]-red.png";
                    }
                    if ($len > 2)
                    {
                        $re['score3'] = "/zhunlemini/astro_card/$num[2]-red.png";
                    }
                } elseif ($re['id'] === 1)
                {
                    $re['score1'] = "/zhunlemini/astro_card/$num[0]-orange.png";
                    if ($len > 1)
                    {
                        $re['score2'] = "/zhunlemini/astro_card/$num[1]-orange.png";
                    }
                    if ($len > 2)
                    {
                        $re['score3'] = "/zhunlemini/astro_card/$num[2]-orange.png";
                    }
                } elseif ($re['id'] === 2)
                {
                    $re['score1'] = "/zhunlemini/astro_card/$num[0]-blue.png";
                    if ($len > 1)
                    {
                        $re['score2'] = "/zhunlemini/astro_card/$num[1]-blue.png";
                    }
                    if ($len > 2)
                    {
                        $re['score3'] = "/zhunlemini/astro_card/$num[2]-blue.png";
                    }
                }
                $power[] = $re;
            }
            $resData = [
                'user_des' => $res['name'],
                'surface_des' => "表面：{$res['surface']}",
                'actual_des' => "实则：{$res['inherent']}",
                'attr_score' => $res['attr'],
                'ability_score' => $power,
                'love_concept' => $res['love_concept'],
                'unique_skills' => [
                    'title' => $res['skill'],
                    'list' => $res['keyword'],
                ],
            ];
            Cache::set($cacheKey, $resData, 172800);
        }

        return $resData;
    }

    /**
     * 星座卡片 - 四象卡
     * @return array
     */
    public static function fourElephantCard(array $recodeInfo)
    {
        $recodeHashStr = md5(json_encode($recodeInfo, JSON_UNESCAPED_UNICODE));
        $cacheKey = 'astro:astro_card:four_elephant_card:' . $recodeInfo['id'] . ':' . $recodeHashStr;
        $resData = Cache::get($cacheKey, false);
        if (!$resData)
        {   // 缓存
            $resData = (new AstroCard())->getElementData($recodeInfo);

            Cache::set($cacheKey, $resData, 172800);
        }

        return $resData;
    }

    /**
     * 星座卡片 - 说明书
     * @return array
     */
    public static function instructions(array $recodeInfo)
    {
        $recodeHashStr = md5(json_encode($recodeInfo, JSON_UNESCAPED_UNICODE));
        $cacheKey = 'astro:astro_card:instructions:' . $recodeInfo['id'] . ':' . $recodeHashStr;
        $resData = Cache::get($cacheKey, false);
        if (!$resData)
        {   // 缓存
            $resData = (new AstroCard())->getInstructionData($recodeInfo);

            Cache::set($cacheKey, $resData, 172800);
        }
        return $resData;
    }
}